<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="健身部位" prop="basicParentDesc">
        <el-input
          v-model="queryParams.basicParentDesc"
          placeholder="请输入健身部位"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="健身类型" prop="basicTypeDesc">
        <el-input
          v-model="queryParams.basicTypeDesc"
          placeholder="请输入健身类型"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="健身量" prop="basicAmount">
        <el-input
          v-model="queryParams.basicAmount"
          placeholder="请输入健身量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="健身组数" prop="basicGroup">
        <el-input
          v-model="queryParams.basicGroup"
          placeholder="请输入健身组数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="健身单位" prop="basicUnitDesc">
        <el-input
          v-model="queryParams.basicUnitDesc"
          placeholder="请输入健身单位"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['fitness:basic:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['fitness:basic:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['fitness:basic:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['fitness:basic:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-if="this.basicList.length != 0" v-loading="loading" :data="basicList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="健身部位" align="center" prop="basicParentDesc" />
      <el-table-column label="健身类型" align="center" prop="basicTypeDesc" />
      <el-table-column label="健身量" align="center" prop="basicAmount" />
      <el-table-column label="健身组数" align="center" prop="basicGroup" />
      <el-table-column label="健身单位" align="center" prop="basicUnitDesc" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['fitness:basic:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['fitness:basic:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div v-if="this.basicList.length == 0 && this.loading == false" v-loading="loading">
      <span align="center">检测到您没有健身项目，是否需要导入基本项目？</span>
      <el-button
        type="primary"
        plain
        icon="el-icon-plus"
        @click="handleImport"
        v-hasPermi="['fitness:basic:add']"
      >导入</el-button>
    </div>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改我的习惯对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="健身部位" prop="basicParent" v-if="basicParentFlag">
          <el-select v-model="form.basicParent" placeholder="健身部位">
            <el-option
            v-for="(value, key) in this.placeData" :key="key"
              :label="value"
              :value="key"
            ></el-option>
          </el-select>
          <el-button
            type="primary"
            plain
            size="mini"
            @click="diy('basicParentFlag')"
            v-hasPermi="['fitness:basic:add']"
          >自定义</el-button>
        </el-form-item>
        <div v-if="basicParentFlag == false">
          <el-form-item label="健身部位" prop="basicParent">
            <el-input v-model="form.basicParent" placeholder="请选择健身类型，例：back" />
            <el-button
            type="primary"
            plain
            size="mini"
            @click="diy('basicParentFlag')"
            v-hasPermi="['fitness:basic:add']"
          >返回</el-button>
          </el-form-item>
          <el-form-item label="健身部位-中文" prop="basicParentDesc">
            <el-input v-model="form.basicParentDesc" placeholder="请输入健身部位中文，例：背部" />
          </el-form-item>
        </div>
        <el-form-item label="健身类型" prop="basicType" v-if="basicTypeFlag">
          <el-select v-model="form.basicType" placeholder="健身类型">
            <el-option
            v-for="(value, key) in this.typeData" :key="key"
              :label="value"
              :value="key"
            ></el-option>
          </el-select>
          <el-button
            type="primary"
            plain
            size="mini"
            @click="diy('basicTypeFlag')"
            v-hasPermi="['fitness:basic:add']"
          >自定义</el-button>
        </el-form-item>
        <div v-if="basicTypeFlag == false">
          <el-form-item label="健身类型" prop="basicType">
            <el-input v-model="form.basicType" placeholder="请输入健身类型，例：barbellBoat" />
            <el-button
            type="primary"
            plain
            size="mini"
            @click="diy('basicTypeFlag')"
            v-hasPermi="['fitness:basic:add']"
          >返回</el-button>
          </el-form-item>
          <el-form-item label="健身类型-中文" prop="basicTypeDesc">
            <el-input v-model="form.basicTypeDesc" placeholder="请输入健身类型中文，例：杠铃划船" />
          </el-form-item>
        </div>
        <el-form-item label="健身量" prop="basicAmount">
          <el-input-number v-model="form.basicAmount" placeholder="请输入健身量" :min="1"/>
        </el-form-item>
        <el-form-item label="健身组数" prop="basicGroup">
          <el-input-number v-model="form.basicGroup" placeholder="请输入健身组数，单组运动可为0" :min="0"/>
        </el-form-item>
        <el-form-item label="健身单位" prop="basicUnit" v-if="basicUnitFlag">
          <el-select v-model="form.basicUnit" placeholder="健身单位">
            <el-option
            v-for="(value, key) in this.unitData" :key="key"
              :label="value"
              :value="key"
            ></el-option>
          </el-select>
          <el-button
            type="primary"
            plain
            size="mini"
            @click="diy('basicUnitFlag')"
            v-hasPermi="['fitness:basic:add']"
          >自定义</el-button>
        </el-form-item>
        <div v-if="basicUnitFlag == false">
          <el-form-item label="健身单位" prop="basicUnit">
            <el-input v-model="form.basicUnit" placeholder="请输入健身单位，例：min" />
            <el-button
            type="primary"
            plain
            size="mini"
            @click="diy('basicUnitFlag')"
            v-hasPermi="['fitness:basic:add']"
          >返回</el-button>
          </el-form-item>
          <el-form-item label="健身单位-中文" prop="basicUnitDesc">
            <el-input v-model="form.basicUnitDesc" placeholder="请输入健身单位中文，例：分钟" />
          </el-form-item>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBasic, getBasic, delBasic, addBasic, updateBasic, dictBasic, importBasic } from "@/api/fitness/basic";

export default {
  name: "Basic",
  dicts: ['plan_fitness_status', 'plan_fitness_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedFitness: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 我的习惯表格数据
      basicList: [],
      // fitness表格数据
      fitnessList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        basicParent: null,
        basicParentDesc: null,
        basicType: null,
        basicTypeDesc: null,
        basicAmount: null,
        basicGroup: null,
        basicUnit: null,
        basicUnitDesc: null,
      },
      // 表单参数
      form: {},
      placeData : [],
      typeData : [],
      unitData : [],      
      basicParentFlag: true,
      basicTypeFlag:true,
      basicUnitFlag:true,
      // 表单校验
      rules: {
        basicParent: [
          { required: true, message: "健身部位不能为空", trigger: "change" }
        ],
        basicParentDesc: [
          { required: true, message: "健身部位-中文不能为空", trigger: "change" }
        ],
        basicType: [
          { required: true, message: "健身类型不能为空", trigger: "change" }
        ],
        basicTypeDesc: [
          { required: true, message: "健身类型-中文不能为空", trigger: "change" }
        ],
        basicAmount: [
          { required: true, message: "健身量不能为空", trigger: "blur" }
        ],
        basicUnit: [
          { required: true, message: "健身单位不能为空", trigger: "change" }
        ],
        basicUnitDesc: [
          { required: true, message: "健身单位-中文不能为空", trigger: "change" }
        ],
        createBy: [
          { required: true, message: "创建者不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询我的习惯列表 */
    getList() {
      this.loading = true;
      listBasic(this.queryParams).then(response => {
        this.basicList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      dictBasic(this.queryParams).then(response => {
        this.placeData = response.place_map;
        this.typeData = response.type_map;
        this.unitData = response.unit_map;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        basicId: null,
        basicParent: null,
        basicParentDesc: null,
        basicType: null,
        basicTypeDesc: null,
        basicAmount: null,
        basicGroup: null,
        basicUnit: null,
        basicUnitDesc: null,
        createBy: null,
        createTime: null
      };
      this.fitnessList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.basicId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加我的习惯";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const basicId = row.basicId || this.ids
      getBasic(basicId).then(response => {
        this.form = response.data;
        this.fitnessList = response.data.fitnessList;
        this.open = true;
        this.title = "修改我的习惯";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.fitnessList = this.fitnessList;
          if (this.form.basicId != null) {
            updateBasic(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBasic(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const basicIds = row.basicId || this.ids;
      this.$modal.confirm('是否确认删除我的习惯编号为"' + basicIds + '"的数据项？').then(function() {
        return delBasic(basicIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** fitness序号 */
    rowFitnessIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 复选框选中数据 */
    handleFitnessSelectionChange(selection) {
      this.checkedFitness = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('fitness/basic/export', {
        ...this.queryParams
      }, `basic_${new Date().getTime()}.xlsx`)
    },
    diy(type) {
      this[type] = (this[type] == false) ? true : false;
    },
    handleImport() {
      this.loading = true;
      importBasic(this.queryParams).then(response => {
        this.getList();
        this.loading = false;
      });
    }
  }
};
</script>
